package com.xbongbong.paas.service.log.crm.operation.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.service.log.crm.help.OperationLogHelp;
import com.xbongbong.paas.service.log.crm.operation.AbstractCrmOperationLogHandle;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.formdata.pojo.vo.BeforeUpdateVO;
import com.xbongbong.pro.formdata.pojo.vo.SaveVO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.pro.log.pojo.dto.AddOperationLogDTO;
import com.xbongbong.pro.log.pojo.dto.UpdateOperationLogDTO;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/4/25 10:57
 * @description:
 */
@Slf4j
@Service("communicatePlanOperationLogHandleImpl")
public class CommunicatePlanOperationLogHandleImpl extends AbstractCrmOperationLogHandle {
    @Resource
    @Lazy
    private OperationLogHelp operationLogHelp;
    @Resource
    private MongoLogHelp mongoLogHelp;
    @Resource
    private PaasEsModel paasEsModel;
    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode());
    }

    @Override
    @Async("sysLogThreadPool")
    public void add(AddOperationLogDTO addOperationLogDTO) {
        try {
            FormDataAddDTO formDataAddDTO = addOperationLogDTO.getFormDataAddDTO();
            PaasFormDataEntity newPaasFormDataEntity = addOperationLogDTO.getNewPaasFormDataEntity();
            SaveVO saveVO = addOperationLogDTO.getSaveVO();
            String corpid = formDataAddDTO.getCorpid();
            Long dataId = saveVO.getDataId();
            boolean isImport = Objects.equals(1, formDataAddDTO.getIsImport());
            if (isImport) {
                return;
            }
            JSONObject data = newPaasFormDataEntity.getData();
            Long customerId = FastJsonHelper.getLongOrDefaultFromFormData(data, CommunicatePlanEnum.LINK_CUSTOMER.getAttr(), 0L);
            PaasFormDataEntityExt paasFormDataEntityExt = paasEsModel.getByKey(customerId, corpid, IndexTypeEnum.IDX_SAAS_CUSTOMER, null);
            String customerName = OperationLogHelp.getName(XbbRefTypeEnum.CUSTOMER_MANAGEMENT,paasFormDataEntityExt.getData());
            String logName = OperationLogHelp.getName(type(),data);
            OperateModuleTypeEnum operateModuleTypeEnum = operationLogHelp.getOperateModule(type());
            //用户“%s”给%s“%s”新增了访客计划“%s”
            UserVO userVO = saveVO.getUserVO();
            String userName = userVO.getName();
            String userId = userVO.getUserId();
            String paasFormName = getPaasFormName(corpid, paasFormDataEntityExt.getFormId(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
            String memo = String.format(I18nMessageUtil.getMessage(CrmLogMemoConstant.COMMUNICATE_PLAN_LOG_MEMO_ADD),
                    userName, paasFormName, customerName,logName);
            mongoLogHelp.buildLog(corpid, userId, userName, operateModuleTypeEnum, OperateTypeEnum.NEW,
                    dataId.toString(), logName, memo, formDataAddDTO.getHttpHeader());
        } catch (XbbException e) {
            log.error("com.xbongbong.paas.service.log.crm.operation.impl.CommunicatePlanOperationLogHandleImpl.add", e);
        }
    }

    @Override
    @Async("sysLogThreadPool")
    public void update(UpdateOperationLogDTO updateOperationLogDTO) {
        try {
            FormDataUpdateDTO formDataUpdateDTO = updateOperationLogDTO.getFormDataUpdateDTO();
            UpdateVO updateVO = updateOperationLogDTO.getUpdateVO();
            BeforeUpdateVO beforeUpdateVO = updateOperationLogDTO.getBeforeUpdateVO();
            RedundantTemplateTypeEnum redundantTemplateTypeEnum = RedundantTemplateTypeEnum.getByCode(type().get(0));
            OperateModuleTypeEnum operateModuleTypeEnum = operationLogHelp.getOperateModule(type());
            PaasFormDataEntity paasFormDataEntity = updateVO.getPaasFormDataEntity();
            String opObjectName = OperationLogHelp.getName(type(),paasFormDataEntity.getData());
            String userName = updateVO.getUserVO().getName();
            String memoName = beforeUpdateVO.getPaasFormEntityExt().getName();
            String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_UPDATE_WORK), userName, memoName, opObjectName);
            updateSaveLogger(formDataUpdateDTO, paasFormDataEntity, redundantTemplateTypeEnum, memo, opObjectName, operateModuleTypeEnum, updateVO, beforeUpdateVO, OperateTypeEnum.EDIT, null);
            } catch (XbbException e) {
            log.error("com.xbongbong.paas.service.log.crm.operation.impl.CommunicatePlanOperationLogHandleImpl.update", e);
        }
    }
}
